package basic;

import java.util.ArrayList;

import com.google.common.base.Objects;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
/**
 * Objects在jdk7.0中有同样的实现。
 * 它帮助生成Object的几个常用方法的值
 * @author tiangh
 * 2013-5-26 下午2:50:08
 */
public class ObjectsDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		fistNotNullDemo();
	}
	/**
	 * 判断输入值是否为null，若为null，则返回第二个默认值，默认值为null抛异常
	 */
	public static void fistNotNullDemo() {
		String inputName=null;
		String defaultName="张三";
		String name=null;
		name=Objects.firstNonNull(inputName, defaultName);
		System.out.println(name);
	}
	
	public static ArrayList<Foo> data = Lists.newArrayList(
			new Foo("aaa", 1,"打赏点啊"),
			new Foo("bfdf", 9,"asa"),
			new Foo("木插画", -1,"哈哈"),
			new Foo("你好",13,"打赏点啊"),
			new Foo(null,13,"打赏点啊"),
			new Foo("你好",7,"打赏点啊")
			);
	
	public static class Foo implements Comparable<Foo>{
		public String A;
		public int B;
		public String C;
		
		public Foo() {
		}

		public Foo(String a, int b, String c) {
			A = a;
			B = b;
			C = c;
		}
		
		@Override
		public int hashCode() {
			return Objects.hashCode(A,B,C);
		}
		
		@Override
		public boolean equals(Object obj) {
			return Objects.equal(this, obj);
		}

		@Override
		public String toString() {
			return Objects.toStringHelper(this).add("A", A).add("B", B).add("C", C).toString();
		}

		@Override
		public int compareTo(Foo that) {
			if (that==null) {
				return 1;
			}
			Ordering<String> ordering = Ordering.natural().nullsLast();
			return ComparisonChain.start().compare(A,that.A,ordering).compare(C,that.C,ordering).compare(B, that.B).result();
		}
	}

}
